package com.kyivmohyla.kma2.dao;

import com.kyivmohyla.kma2.dao.mapper.EquipmentRowMapper;
import com.kyivmohyla.kma2.model.Equipment;
import com.kyivmohyla.kma2.model.Service;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.util.ArrayList;
import java.util.List;

public class EquipmentDaoJdbc extends JdbcDaoSupport implements EquipmentDao {

    @Override
    public Equipment getById(int id) throws DataAccessException {
        return getJdbcTemplate()
                .queryForObject(
                        "SELECT equipment.equipment_id, equipment.name, equipment.is_parent, equipment.parent_id, equipment.description FROM equipment WHERE equipment.equipment_id = ?",
                        new Object[]{id}, new EquipmentRowMapper());
    }

    @Override
    public List<Equipment> getAll() throws DataAccessException {
        return getJdbcTemplate()
                .query("SELECT equipment.equipment_id, equipment.name, equipment.is_parent, equipment.parent_id, equipment.description FROM equipment ORDER BY equipment.equipment_id",
                        new EquipmentRowMapper());
    }

    @Override
    public void create(Equipment equipment) throws DataAccessException {
        getJdbcTemplate()
                .update("INSERT INTO equipment (equipment.equipment_id, equipment.name, equipment.is_parent, equipment.parent_id, equipment.description) VALUES (serv_equip_seq.NEXTVAL, ?, ?, ?, ?)",
                        equipment.getName(),
                        equipment.getIsParent(),
                        equipment.getParentId() == 0 ? null
                                : equipment.getParentId(),
                        equipment.getDescription());
    }

    @Override
    public void update(Equipment equipment) throws DataAccessException {
        getJdbcTemplate()
                .update("UPDATE equipment SET equipment.name = ?, equipment.is_parent = ?, equipment.parent_id = ?, equipment.description = ? WHERE equipment.equipment_id = ?",
                        equipment.getName(),
                        equipment.getIsParent(),
                        equipment.getParentId() == 0 ? null
                                : equipment.getParentId(),
                        equipment.getDescription(),
                        equipment.getEquipmentId());
    }

    @Override
    public void delete(Equipment equipment) throws DataAccessException {
        getJdbcTemplate().update(
                "DELETE FROM equipment WHERE equipment.equipment_id = ?",
                equipment.getEquipmentId());
    }

    @Override
    public void deleteList(List<Equipment> equipmentList) throws DataAccessException {
        for (Equipment equipment : equipmentList) {
            delete(equipment);
        }
    }

    @Override
    public boolean canBeChildDeleted(Equipment equipment) throws DataAccessException {
        List<Equipment> couldNoBeDeletedList = getJdbcTemplate().query("SELECT equipment.equipment_id, equipment.name, equipment.is_parent, equipment.parent_id, equipment.description FROM equipment WHERE equipment.is_parent = 0 AND equipment.equipment_id in (SELECT order_serv_equip.serv_equip_id FROM order_serv_equip UNION SELECT refs.equipment_id FROM refs)",
                new EquipmentRowMapper());
        return !couldNoBeDeletedList.contains(equipment);
    }

    @Override
    public boolean canBeParentDeleted(Equipment equipment) throws DataAccessException {
        List<Equipment> children = getJdbcTemplate().query("SELECT equipment.equipment_id, equipment.name, equipment.is_parent, equipment.parent_id, equipment.description FROM equipment WHERE equipment.parent_id = ?", new Object[]{equipment.getEquipmentId()}, new EquipmentRowMapper());
        return children.size() == 0;
    }

    @Override
    public List<Equipment> getEquipmentByServicesList(List<Service> services) throws DataAccessException {
        List<Equipment> equipments = new ArrayList<Equipment>();
        for (Service service : services) {
            try {
                List<Equipment> result = getJdbcTemplate()
                        .query(
                                "SELECT equipment.equipment_id, equipment.name, equipment.is_parent, equipment.parent_id, equipment.description FROM equipment WHERE equipment.equipment_id IN (SELECT refs.equipment_id FROM refs WHERE refs.service_id = ?)",
                                new Object[]{service.getServiceId()}, new EquipmentRowMapper());
                equipments.removeAll(result);
                equipments.addAll(result);
            } catch (DataAccessException e) {
                e.printStackTrace();
            }
        }
        return equipments;
    }

    @Override
    public List<Equipment> getEquipmentByOrderId(int id) throws DataAccessException {
        return getJdbcTemplate().query("SELECT equipment.equipment_id, equipment.name, equipment.is_parent, equipment.parent_id, equipment.description FROM equipment WHERE equipment.equipment_id IN (SELECT order_serv_equip.serv_equip_id FROM order_serv_equip WHERE order_serv_equip.order_id = ?)", new Object[]{id}, new EquipmentRowMapper());
    }
}
